package com.android.phpclient;

import java.util.ArrayList;
import java.util.List;

import com.utez.sistemas.sam.R;

import Clases.calificacion;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Calificaciones_Alumn extends Activity implements OnClickListener,OnItemSelectedListener{

	public List<String> Lcalif =new ArrayList<String>();
	TextView alumno,matricula;
	Button ordi,extra,recupera,ingresar;
	String idTipos,Acalif_L;
	String idU,idM,idG,method;
	int Acalif_N=0;
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ingresar_calif);
        
        alumno=(TextView) findViewById(R.id.alumno);
        matricula=(TextView) findViewById(R.id.matricula);
        Bundle bundle=getIntent().getExtras();
       alumno.setText(""+bundle.getString("nombreA"));
  
        matricula.setText(""+bundle.getString("matriA"));
        calificacion c=new calificacion();
        
        
        method=bundle.getString("method");
        idU=bundle.getString("IDu");
        idM=bundle.getString("IDm");
        idG=bundle.getString("IDg");
        
        Lcalif.addAll(c.calif());
        
        ArrayAdapter<String> adaptador = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, Lcalif);
        
    	final Spinner combo = (Spinner)findViewById(R.id.calificaciones);
    	adaptador.setDropDownViewResource(
    	android.R.layout.simple_spinner_dropdown_item);
    	combo.setAdapter(adaptador);
    	
    	combo.setOnItemSelectedListener(this);
    	
    	ordi=(Button) findViewById(R.id.ordinario);
    	extra=(Button) findViewById(R.id.extra);
    	recupera=(Button) findViewById(R.id.recupera);
    	ingresar=(Button) findViewById(R.id.Ingresa);
    	ordi.setOnClickListener(this);
    	extra.setOnClickListener(this);
    	recupera.setOnClickListener(this);
    	
    	
    	ingresar.setOnClickListener(this);
 }
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId()==R.id.ordinario){
			idTipos=""+1;
		}
		if(v.getId()==R.id.extra){
			idTipos=""+3;
		}
		if(v.getId()==R.id.recupera){
			idTipos=""+2;
		}
		
		
		if(v.getId()==R.id.Ingresa){
			ServerAccess ss=new ServerAccess();
			   calificacion c=new calificacion();
			   
			Acalif_N=c.ValidadCalif(Acalif_L);
			
			
			//String todo=Acalif_L+" \n "+Acalif_N+" \n "+idU+" \n "+idTipos+" \n "+idM+" \n "+matricula.getText().toString(); 
			//Toast.makeText(getApplicationContext(), ""+method, Toast.LENGTH_LONG).show();
			
			if(method.equals("insert")){
			c.message=ss.Ingresar_calificacion(Acalif_L, Acalif_N, idU, idTipos, idM, matricula.getText().toString());
			
			}
			if(method.equals("Modificar")){
				c.message=ss.modificar_calificacion(Acalif_L, Acalif_N, idU, idTipos, idM, matricula.getText().toString());
			}
	        
             
                Toast toast = Toast.makeText(getApplicationContext(), ""+c.message+"", Toast.LENGTH_SHORT);
        		toast.setGravity(Gravity.AXIS_SPECIFIED, 0, 0);
        		toast.show();   
        		finish();
		}
	}
	@Override
	public void onItemSelected(AdapterView<?> v, View arg1, int location,
			long arg3) {
		// TODO Auto-generated method stub
		
		if(v.getId()==R.id.calificaciones){
			Acalif_L=Lcalif.get(location);
		//	Toast.makeText(getApplicationContext(), ""+Acalif_L, Toast.LENGTH_LONG).show();
		}
	}
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
	
}
